/*
    2. Write a C program to find the length of the longest substring of 
    a given string without repeating characters. 
    Expected Output:

    Original String: xyzxyzyy  
    Length of the longest substring without repeating characters: 3
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int length_of_longest_substring(char *str){
    int offset[128];
    int substr_max_len = 0;
    int len = 0;
    int index = 1;

    memset(offset, 0, sizeof(offset));
    while (*str != '\0'){
        if (offset[*str] == 0){
            len++;
        }else{
            if (index - offset[*str] > len){
                len++;
            }else{
                len = index - offset[*str];
            }
        }
        printf("idx = %d, len = %d\n", index, len);
        if (len > substr_max_len){
            substr_max_len = len;
        }
        offset[*str++] = index++;
    }

    return substr_max_len;
}

int main(void)
{
    char *str = "abccadfeghijklaal";
    printf("Original String: %s\n", str);
    printf("Length of the longest substring without repeating characters: %d", length_of_longest_substring(str));

    return 0;
}